<template>
  <div class="home">
    <!-- 头部导航 -->
    <van-nav-bar fixed class="page-nav-bar">
      <template #title>
        <van-button to="/search" class="search-btn" icon="search">搜索</van-button>
      </template>
    </van-nav-bar>
    <!-- 滑动导航 -->
    <van-tabs
      sticky
      offset-top="1.22rem"
      class="home-tabs"
      v-model="active"
      animated
      swipeable
      :swipe-threshold="3"
    >
      <van-tab v-for="item in Channels" :key="item.id" :title="item.name">
        <article-list :channel_id="item.id" />
      </van-tab>
      <template #nav-right>
        <div class="placeholder-box"></div>
        <div class="right-menu" @click="isShowEditPopup = true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <van-popup
      class="editPopup"
      v-model="isShowEditPopup"
      close-icon-position="top-left"
      closeable
      position="bottom"
      :style="{ height: '80%' }"
    >
    <channel-edit @onGo="onGo" @setActive="active--" :channels="Channels" :active="active"/>
    </van-popup>
  </div>
</template>

<script>
import { getStorage } from '@/utils/storage.js'
import { getUserChannels } from '@/api/user.js'
import { Toast } from 'vant'
import ArticleList from './components/ArticleList'
import ChannelEdit from './components/ChannelEdit.vue'
export default {
  name: 'HOME',
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0,
      Channels: [],
      isShowEditPopup: false
    }
  },

  created () {
    this.loadUserChannels()
  },

  methods: {
    async loadUserChannels () {
      const storage = getStorage('MY_CHANNEL')
      if (!this.$store.user && storage) {
        this.Channels = storage
        return
      }
      try {
        const res = await getUserChannels()
        this.Channels = res.channels
      } catch (err) {
        Toast.fail('获取用户频道失败')
      }
    },
    onGo (e) {
      this.active = e
      this.isShowEditPopup = false
    }
  }
}
</script>

<style scoped lang="less">
.home {
  .search-btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 32px;
    color: #fff;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .van-icon-search {
    color: #fff;
  }
  /deep/.home-tabs {
    padding-top: 92px;
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }
    .van-tabs__wrap {
      height: 82px;
      border-bottom: 1px solid #edeff3;
    }
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296fa;
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav--line {
      padding: 0;
    }
    .right-menu {
      position: fixed;
      right: 0;
      height: 82px;
      width: 66px;
      // background: rgba(255, 255, 255, 0.1);
      text-align: center;
      i {
        font-size: 40px;
        line-height: 82px;
      }
      &::before {
        content: "";
        width: 1px;
        height: 58px;
        position: absolute;
        left: 0;
        top: 12px;
        background-image: url(~@/assets/images/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    .placeholder-box {
      width: 66px;
      flex-shrink: 0;
    }
  }
  .editPopup {
    padding: 100px 0 0;
  }
}
</style>
